package com.cdtu.labpreview.controller;

import com.cdtu.labpreview.common.Result;
import com.cdtu.labpreview.common.UserHolder;
import com.cdtu.labpreview.dto.UserDTO;
import com.cdtu.labpreview.entity.Reference;
import com.cdtu.labpreview.service.IReferenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 参考资料模块
 */
@RestController
@RequestMapping("/reference")
public class ReferenceController {
    @Autowired
    private IReferenceService pictureDisplayService;

    /**
     * 查看参考资料（老师、学生）
     * @return
     */
    @GetMapping
    public Result getPictureDisplay(){
        UserDTO userDTO = UserHolder.getUser();
        int no = userDTO.getUserNo();
        int type = userDTO.getType();
        return pictureDisplayService.getReference(no,type);
    }

    /**
     * 添加参考资料（老师）
     * @param pictureDisplay
     * @return
     */
    @PostMapping
    public Result addPictureDisplay(@RequestBody Reference pictureDisplay){
        pictureDisplay.setTno(UserHolder.getUser().getUserNo());
        return pictureDisplayService.addReference(pictureDisplay);
    }

    /**
     * 修改参考资料（老师）
     * @param pictureDisplay
     * @return
     */
    @PutMapping
    public Result updatePictureDisplay(@RequestBody Reference pictureDisplay){
        return pictureDisplayService.updateReference(pictureDisplay);
    }

    /**
     * 删除参考资料（老师）
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deletePictureDisplay(@PathVariable("id") long id){
        return pictureDisplayService.deleteReference(id);
    }
}
